// utils/crypto.js
import CryptoJS from 'crypto-js'

const key = CryptoJS.enc.Utf8.parse('nnbnqs2025wfzzzz') // 16位密钥
const iv = CryptoJS.enc.Utf8.parse('bdx123456789dcyy') // 16位偏移量

function encryptFileName(name) {
    const ext = name.substring(name.lastIndexOf('.'))
    const pureName = name.substring(0, name.lastIndexOf('.'))
    const encrypted = CryptoJS.AES.encrypt(pureName, key, {iv}).toString()
    return encodeURIComponent(encrypted) + ext
}

function decryptFileName(encryptedName) {
    const ext = encryptedName.substring(encryptedName.lastIndexOf('.'))
    const encrypted = decodeURIComponent(encryptedName.substring(0, encryptedName.lastIndexOf('.')))
    const decrypted = CryptoJS.AES.decrypt(encrypted, key, {iv}).toString(CryptoJS.enc.Utf8)
    return decrypted + ext
}

export default {encryptFileName, decryptFileName}
